package com.google.code.ptrends.Application.DAL.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "APP.SESSIONS")
public class SessionEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private int id;
	private SupplierEntity supplier;
	private Date startDate, endDate;
	private List<SessionEventEntity> sessionEvents = new ArrayList<SessionEventEntity>();
	private EventTypeEntity state;
	private int itemsCount;
	@Transient
	private String duration;

	public void setId(int id) {
		this.id = id;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public int getId() {
		return id;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate == null ? null : new Date(
				startDate.getTime());
	}

	@Column(name = "START_DATE", nullable = false)
	public Date getStartDate() {
		Date cloned = null;
		if (startDate != null) {
			cloned = new Date(startDate.getTime());
		}
		return cloned;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate == null ? null : new Date(endDate.getTime());
	}

	@Column(name = "END_DATE", nullable = false)
	public Date getEndDate() {
		Date cloned = null;
		if (endDate != null) {
			cloned = new Date(endDate.getTime());
		}
		return cloned;
	}

	public void setSessionEvents(List<SessionEventEntity> sessionEvents) {
		this.sessionEvents = sessionEvents;
	}

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, targetEntity = SessionEventEntity.class)
	@JoinTable(name = "SESSION_EVENTS", joinColumns = @JoinColumn(name = "SESSIONS_ID"), inverseJoinColumns = @JoinColumn(name = "ID"))
	public List<SessionEventEntity> getSessionEvents() {
		return sessionEvents;
	}

	public void setState(EventTypeEntity state) {
		this.state = state;
	}

	@ManyToOne
	@Embedded
	@JoinColumn(name = "EVENT_TYPE_ID", nullable = false)
	public EventTypeEntity getState() {
		return state;
	}

	public void setItemsCount(int itemsCount) {
		this.itemsCount = itemsCount;
	}

	@Column(name = "ITEMS_COUNT", nullable = false)
	public int getItemsCount() {
		return itemsCount;
	}

	public void setSupplier(SupplierEntity supplier) {
		this.supplier = supplier;
	}

	@ManyToOne
	@Embedded
	@JoinColumn(name = "SUPPLIER_ID", nullable = false)
	public SupplierEntity getSupplier() {
		return supplier;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}

	@Transient
	public String getDuration() {
		if (startDate == null || endDate == null)
			return "";
		Calendar date = Calendar.getInstance();
		date.setTimeInMillis(startDate.getTime());
		Calendar eDate = Calendar.getInstance();
		eDate.setTime(endDate);
		int between = 0;
		while (date.before(eDate)) {
			date.roll(Calendar.SECOND, true);
			between++;
		}
		int minutes = (int) (between / 60 % 60);
		int seconds = (int) (between % 60);
		duration = String.valueOf(minutes) + "m " + String.valueOf(seconds)
				+ "s";
		return duration;
	}

}
